package cn.fu.aop.annotation;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AdviceTest {
	  @Around("execution(* cn.fu.core.service.*.insert*(..))")
	    public Object process(ProceedingJoinPoint point) throws Throwable {
	        System.out.println("@Around：执行目标方法之前...");
	        //访问目标方法的参数：
	        Object[] args = point.getArgs();
	        if (args != null && args.length > 0 && args[0].getClass() == String.class) {
	            args[0] = "改变后的参数1";
	        }
	        //AOP目的用于向库中自动加入当前时间
	        if(args != null && args.length > 0){
	        	Class a=args[0].getClass();
	        	Method[] methods = a.getDeclaredMethods();
	        	for (Method method : methods) {
	        		String name = method.getName();
	        		if(name.equals("setName")){
	        			method.invoke(args[0], "张三");
	        		}
				}
	        }
	        //用改变后的参数执行目标方法
	        Object returnValue = point.proceed(args);
	        System.out.println("@Around：执行目标方法之后...");
	        System.out.println("@Around：被织入的目标对象为：" + point.getTarget());
	        return "原返回值：" + returnValue + "，这是返回结果的后缀";
	    }
	     
	    @Before("execution(* cn.fu.core.service.*.many*(..))")
	    public void permissionCheck(JoinPoint point) {
	        System.out.println("@Before：模拟权限检查...");
	        System.out.println("@Before：目标方法为：" + 
	                point.getSignature().getDeclaringTypeName() + 
	                "." + point.getSignature().getName());
	        System.out.println("@Before：参数为：" + Arrays.toString(point.getArgs()));
	        System.out.println("@Before：被织入的目标对象为：" + point.getTarget());
	    }
	     
	    @AfterReturning(pointcut="execution(* cn.fu.core.service.*.many*(..))", 
	        returning="returnValue")
	    public void log(JoinPoint point, Object returnValue) {
	        System.out.println("@AfterReturning：模拟日志记录功能...");
	        System.out.println("@AfterReturning：目标方法为：" + 
	                point.getSignature().getDeclaringTypeName() + 
	                "." + point.getSignature().getName());
	        System.out.println("@AfterReturning：参数为：" + 
	                Arrays.toString(point.getArgs()));
	        System.out.println("@AfterReturning：返回值为：" + returnValue);
	        System.out.println("@AfterReturning：被织入的目标对象为：" + point.getTarget());
	         
	    }
	     
	    @After("execution(* cn.fu.core.service.*.many*(..))")
	    public void releaseResource(JoinPoint point) {
	        System.out.println("@After：模拟释放资源...");
	        System.out.println("@After：目标方法为：" + 
	                point.getSignature().getDeclaringTypeName() + 
	                "." + point.getSignature().getName());
	        System.out.println("@After：参数为：" + Arrays.toString(point.getArgs()));
	        System.out.println("@After：被织入的目标对象为：" + point.getTarget());
	    }
	    
	    /*{dataSource-1} inited
	    @Around：执行目标方法之前...
	    method AfterReturning
	    @Around：执行目标方法之后...
	    @Around：被织入的目标对象为：cn.fu.core.service.TestTbServiceImpl@358cd06f*/
}
